package com.yanqu.road.server.cmd.player;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eMessageType;
import com.yanqu.road.entity.enums.ePlatformForbiddenType;
import com.yanqu.road.entity.servercenter.GameEditLimit;
import com.yanqu.road.entity.servercenter.ServerGroup;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.worldmessage.WorldMessageProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.creditpoints.CreditPointsModule;
import com.yanqu.road.server.gameplayer.module.cricket.CricketModule;
import com.yanqu.road.server.gameplayer.module.player.ChatModule;
import com.yanqu.road.server.manger.*;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.activity.commonteam.CommonTeamMgr;
import com.yanqu.road.server.manger.activity.douluodalu.DouLuoDaLuMgr;
import com.yanqu.road.server.manger.activity.dunhuangshiku.DunHuangShiKuMgr;
import com.yanqu.road.server.manger.activity.fivehero.FiveHeroMgr;
import com.yanqu.road.server.manger.activity.sonhai.SonHaiActivityMgr;
import com.yanqu.road.server.manger.activity.tonbing.TonBingBugMgr;
import com.yanqu.road.server.manger.activity.tonbing.TonBingMgr;
import com.yanqu.road.server.manger.activity.fivehero.FiveHeroBugMgr;
import com.yanqu.road.server.manger.activity.tonbingchannel.ChannelTonBingMgr;
import com.yanqu.road.server.manger.activity.tongbing.ChuHanMgr;
import com.yanqu.road.server.manger.activity.tongbing.TongBingMgr;
import com.yanqu.road.server.manger.activity.washe.WasheMgr;
import com.yanqu.road.server.manger.activity.xiuxian.XiuXianMgr;
import com.yanqu.road.server.manger.cross3.ChannelActivitySwitchMgr;
import com.yanqu.road.server.manger.kuang.KuangMgr;
import com.yanqu.road.server.manger.union.CrossUnionMgr;
import com.yanqu.road.server.manger.zhaoshang.ZsMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.ConfigHelper;
import com.yanqu.road.utils.config.Config;
import com.yanqu.road.utils.string.StringUtils;

import java.util.List;


@Cmd(code = Protocol.S_WORLD_CHAT, desc = "世界聊天")
public class WorldChatCmd extends AbstractCommand {

    private int type;

    private int contentType;

    private String content = "";

    private String banquetId = "";

    private int activityId;

    private List<String> callUserNameList;
    private String groupParam;
    private boolean showIp = false;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        WorldMessageProto.WorldChatReqMsg reqMsg = WorldMessageProto.WorldChatReqMsg.parseFrom(packet.getBytes());
        type = reqMsg.getType();
        contentType = reqMsg.getContentType();
        content = reqMsg.getContent();
        banquetId = reqMsg.getBanquetId();
        activityId = reqMsg.getActivityId();
        callUserNameList = reqMsg.getCallUserNameList();
        groupParam = reqMsg.getGroupParam();
        if(reqMsg.hasShowIp()){
            showIp = reqMsg.getShowIp();
        }
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        // TODO 测试代码
        if (ServerListMgr.getChannelId(GameServer.getInstance().getServerId()) == 666) {
            content = DebugHelper.gmSend(player, content);
        }
        //编辑限制
        GameEditLimit gameEditLimit = GameEditLimitMgr.getGameEditLimit(Protocol.S_WORLD_CHAT);
        if (null != gameEditLimit) {
            long nowTime = System.currentTimeMillis();
            if (nowTime >= gameEditLimit.getLimitEditStartTime() && nowTime < gameEditLimit.getLimitEditEndTime()) {
                ret = GameErrorCode.E_SYSTEM_NO_OPEN;
                return;
            }
        }

        if (ForbiddenMgr.isPlatformForbidden(player.getUserInfo().getPlatformUserId(), ePlatformForbiddenType.CHAT.getType())) {
            return;
        }
        // 判断是否被禁言(指导员跳过)
        if (eMessageType.WorldChat.getValue() == type && !player.getUserInfo().isInstructor() && player.getModule(CreditPointsModule.class).checkBanned()) {
            ret = GameErrorCode.E_CHAT_WAS_BANNED;
            return;
        }
        int serverOpenDay = ServerListMgr.getServerOpenDay(GameServer.getInstance().getServerId());
        if (eMessageType.WorldChat.getValue() == type) {
            if (!player.getUserInfo().isInstructor()) {
                if (player.getTitleId() < ConfigMgr.getChatLimitTitleLv(serverOpenDay, false)) {
                    ret = GameErrorCode.E_CHAT_TITLE_NO_ENOUGH;
                    return;
                }
            }
        } else if (eMessageType.CrossGroupChat.getValue() == type && player.getTitleId() < GameConfig.CROSS_CHAT_NEED_TITLE) {
            ret = GameErrorCode.E_CHAT_TITLE_NO_ENOUGH;
            return;
        } else if (eMessageType.CrossActivityChat.getValue() == type) {
            if (WasheMgr.isWasheActivityChat(activityId) && player.getTitleId() < GameConfig.WASHEPVP_CHAT_NEED_TITLE) {
                ret = GameErrorCode.E_CHAT_TITLE_NO_ENOUGH;
                return;
            }
        } else if (eMessageType.CrossGroupUnionRecruit.getValue() == type) {
            if (player.getTitleId() < GameConfig.CROSS_CHAT_NEED_TITLE) {
                ret = GameErrorCode.E_CHAT_TITLE_NO_ENOUGH;
                return;
            } else if (null == CrossUnionMgr.getUnionInfo(player.getUserInfo().getUnionUid())) {
                ret = GameErrorCode.E_UNION_NO_FOUND;
                return;
            }
        }
        // 判断发言长度
        if (StringUtils.getStringLen(content) > GameConfig.WORLD_CHAT_MESSAGE_MAX_LENGTH) {
            ret = GameErrorCode.E_MESSAGE_OVER_LENGTH;
            return;
        }
        if (eMessageType.CrossGroupChat.getValue() == type || eMessageType.CrossGroupUnionRecruit.getValue() == type) {
            ServerGroup serverGroup = ServerGroupMgr.getServerGroupByServerId(GameServer.getInstance().getServerId());
            if (null == serverGroup || !serverGroup.isCrossChat()) {
                ret = GameErrorCode.E_CROSS_CHAT_NO_OPEN;
                return;
            }
        }
        if (eMessageType.ZhaoShangGroupChat.getValue() == type) {
            if (!ZsMgr.isZsSystemOpenTime()) {
                ret = GameErrorCode.E_CROSS_CHAT_NO_OPEN;
                return;
            }
        }
        if (eMessageType.SystemGroupChat.getValue() == type) {
            //int groupId = SystemCrossServerGroupMgr.getGroupIdByServerId(activityId, GameServer.getInstance().getServerId());
            //ICrossServerGroup serverGroup = SystemCrossServerGroupMgr.getServerGroup(activityId, groupId);
            //if(null == serverGroup || serverGroup.getState() == 0){
            //    ret = GameErrorCode.E_CROSS_CHAT_NO_OPEN;
            //    return;
            //}
        }
        if (eMessageType.DunHuangShiKuTeamChat.getValue() == type) {
            ret = DunHuangShiKuMgr.canEnterActivityEndShowTime(player, activityId);
            if (ret != 0) {
                return;
            }
        }
        if (eMessageType.ChannelActivityChat.getValue() == type) {
            if (!ChannelActivitySwitchMgr.isChannelActivityChatOpen(activityId)) {
                ret = GameErrorCode.E_CHANNEL_CHAT_NO_OPEN;
                return;
            }
        }
        if (eMessageType.DouLuoDaLuTeamChat.getValue() == type) {
            if ((ret = DouLuoDaLuMgr.canEntryActivity(player)) != 0) return;
        }
        if (eMessageType.TongBingPublicChat.getValue() == type
                || eMessageType.TongBingCampChat.getValue() == type
                || eMessageType.TongBingBattlefieldChat.getValue() == type) {
            if (!ActivityMgr.activityInShowTime(TongBingMgr.getActivityInfo())) {
                ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
                return;
            }
        }

        if (eMessageType.ChuHanPublicChat.getValue() == type
                || eMessageType.ChuHanCampChat.getValue() == type
                || eMessageType.ChuHanBattlefieldChat.getValue() == type) {
            if (!ActivityMgr.activityInShowTime(ChuHanMgr.getActivityInfo())) {
                ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
                return;
            }
        }

        if (eMessageType.TonBingPublicChat.getValue() == type
                || eMessageType.TonBingCampChat.getValue() == type
                || eMessageType.TonBingBattlefieldChat.getValue() == type) {
            ActivityInfo activityInfo = NormalActivityMgr.getActivityInfo(activityId);
            if (activityInfo != null && activityInfo.getType() == eActivityType.ChannelTonBingActivity.getValue() &&
                    !ChannelActivitySwitchMgr.isChannelActivityChatOpen(activityId)) {
                ret = GameErrorCode.E_CHANNEL_CHAT_NO_OPEN;
                return;
            }
            //巅峰统兵和跨域统兵一起
            if (!ActivityMgr.activityInShowTime(TonBingMgr.getActivityInfo())
                    && !ActivityMgr.activityInShowTime(ChannelTonBingMgr.getActivityInfo())) {
                ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
                return;
            }
        }
        if (eMessageType.XiuXianPublicChat.getValue() == type
                || eMessageType.XiuXianCampChat.getValue() == type) {
            if (!ActivityMgr.activityInShowTime(XiuXianMgr.getActivityInfo())) {
                ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
                return;
            }
        }
        if (eMessageType.KuangPublicChat.getValue() == type
                || eMessageType.KuangCampChat.getValue() == type) {
            if (KuangMgr.getMatchGroup() == null) {
                ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
                return;
            }
        }
        if (eMessageType.FiveHeroPublicChat.getValue() == type
                || eMessageType.FiveHeroCampChat.getValue() == type) {
            if (!ActivityMgr.activityInShowTime(FiveHeroMgr.getActivityInfo())) {
                ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
                return;
            }
        }
        //通用组队
        if (eMessageType.CommonTeamChat.getValue() == type) {
            ret = CommonTeamMgr.canEntryActivity(player, activityId);
            if (ret != 0) {
                return;
            }
        }
        //跨域报名，屏蔽ip地区
        if(type == eMessageType.EnterNameActivityChat.getValue() || type == eMessageType.EnterNameActivityGroupChat.getValue()){
            showIp = false;
        }
        ret = player.getModule(ChatModule.class).chat(player, type, contentType, content, activityId, banquetId, callUserNameList, groupParam, showIp);

        FiveHeroBugMgr.chatTest(player, content);
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret != 0) {
            WorldMessageProto.WorldChatRespMsg.Builder respMsg = WorldMessageProto.WorldChatRespMsg.newBuilder();
            respMsg.setRet(ret);
            player.sendPacket(Protocol.U_WORLD_CHAT, respMsg);
        }
    }
}
